﻿using System;
using System.Collections.Generic;
using UnityEditor.Experimental.GraphView;
using UnityEngine;

public class SkillManager:MonoBehaviour
{
    [SerializeField]private List<Skill> _skills = new List<Skill>();

    private Dictionary<int, Skill> _skillList = new Dictionary<int, Skill>();
    private AnimationIntermediary _animationIntermediary;
    private Animator _animator;
    private void Awake()
    {
        _animationIntermediary = GetComponent<AnimationIntermediary>();
        _animator = GetComponent<Animator>();
    }
    private void Start()
    {
        Debug.Log(_skills.Count);
        for (int i = 0; i <=_skills.Count-1; i++)
        {
            Debug.Log("添加技能" + _skills[i].SkillConfig.Id);
            RegisterSkill(_skills[i]);
        }
    }

    private void Update()
    {
    
        foreach (Skill skill in _skills) 
        {
            if (Input.GetKeyDown(skill.SkillConfig.Key)) 
            {
                _animator.SetTrigger("UseSkill");
            }
        }
    }

    public void RegisterSkill(Skill skill) 
    {

        if (_skillList.ContainsKey(skill.SkillConfig.Id)) 
        {
            return;
        }
        _skillList.Add(skill.SkillConfig.Id,skill);
        _animationIntermediary.BeginSkillEvent+=skill.PlaySkill;
        _animationIntermediary.OnSkillEvent += skill.OnSkill;
        _animationIntermediary.FinishSkillEvent += skill.SkillComplete;
        _animationIntermediary.EndSkillEvent += skill.SkillEnd;
    }

    public void CancellationSkill(int id) 
    {
        if (_skillList.ContainsKey(id)) 
        {
            _animationIntermediary.BeginSkillEvent -= _skillList[id].PlaySkill;
            _animationIntermediary.OnSkillEvent -= _skillList[id].OnSkill;
            _animationIntermediary.FinishSkillEvent -= _skillList[id].SkillComplete;
            _animationIntermediary.EndSkillEvent -= _skillList[id].SkillEnd;
            _skillList.Remove(id);
        }
        

    }

}

